<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="3">
            <el-form-item label="药品名称：" prop="drugName" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入药品名称" v-model="queryParams.drugName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="质量等级：" prop="qualityLevel" label-width="80">
              <el-select placeholder="请选择" clearable @keyup.enter="handleQuery" v-model="queryParams.qualityLevel"
                filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_drug_quality_level)" :key="dict.value"
                  :label="dict.label" :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="备案证明编号：" prop="code" label-width="110">
              <el-input clearable maxlength="100" placeholder="请输入" @keyup.enter="handleQuery"
                v-model="queryParams.code" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="销售单位：" prop="saleFactory" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入销售单位" @keyup.enter="handleQuery"
                v-model="queryParams.saleFactory" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="生产厂家：" prop="produceFactory" label-width="80">
              <el-input clearable maxlength="100" @keyup.enter="handleQuery" placeholder="请输入生产厂家"
                v-model="queryParams.produceFactory" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="类型：" prop="drugType" label-width="50">
              <el-select placeholder="请选择" clearable v-model="queryParams.drugType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_drug_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-row justify="end">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">化学药品在库列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-row style="margin-bottom: 5px;">
          <el-button type="success" link icon="DocumentDelete" style="margin-left: 20px;"
            @click="handleExport">导出台账</el-button>
          <!-- <el-button type="success" link icon="DocumentDelete" style="margin-left: 20px;">标准物质一览表</el-button> -->
        </el-row>
        <el-table ref="mainTableRef" :data="bzwzTable">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="唯一性编号" prop="drugCode" align="center" />
          <el-table-column label="易制毒药品名称" prop="drugName" align="center" />
          <el-table-column label="类型" prop="drugType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_drug_type" :value="scope.row.drugType" />
            </template>
          </el-table-column>
          <el-table-column label="质量等级" prop="qualityLevel" align="center">
            <template #default="scope">
              <dict-tag :options="sys_drug_quality_level" :value="scope.row.qualityLevel" />
            </template>
          </el-table-column>
          <el-table-column label="销售单位" prop="saleFactory" align="center" />
          <el-table-column label="生产厂商" prop="produceFactory" align="center" />
          <el-table-column label="备案证明编号" prop="code" align="center" />
          <el-table-column label="规格" prop="totalGg" align="center">
            <template #default="scope">
              {{ scope.row.totalGg }}
              <span v-html="transformation(scope.row.ggUnitName)
                "></span>
            </template>
          </el-table-column>
          <el-table-column label="保存位置" prop="saveLocation" align="center" />
          <el-table-column label="入库日期" prop="createDate" align="center" />
          <el-table-column label="在库余量" prop="createCount" align="center">
            <template #default="scope">
              <el-button link icon="EditPen" @click="remainCountClick(scope.row)">{{ scope.row.remainCount }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="remainGgOpen" :title="remainGgTitle" @close="closedRemainGgDialog">
      <el-form :model="remainGgForm" label-width="80">
        <el-form-item label="在库余量：" prop="remainCount">
          <el-input-number clearable placeholder="请输入在库余量" :min="0" style="width: 20%" controls-position="right"
            v-model="remainGgForm.remainCount" />
        </el-form-item>
        <el-form-item label="说明：" prop="remark">
          <el-input clearable type="textarea" placeholder="请输入说明" :autosize="{ minRows: 3, maxRows: 7 }"
            v-model="remainGgForm.remark" maxlength="500" show-word-limit />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="remainGgSave">保 存</el-button>
          <el-button type="danger" @click="remainGgOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HxypLedger">
import {
  newListLedger,
  saveRemainGg,
  getRemainGg,
} from "@/api/resource/hxyp";
import { listUser } from "@/api/contract/check";
import { getUnitList } from "@/api/store/unit";

const { proxy } = getCurrentInstance();
const { sys_drug_type, sys_drug_quality_level } = proxy.useDict(
  "sys_drug_type",
  "sys_drug_quality_level"
);

const dateRange = ref([]);
const bzwzTable = ref([]);
const jcxmTable = ref([]);
const remainGgOpen = ref(false);
const remainGgTitle = ref("编辑余量");
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const total = ref(0);
const open = ref(false);
const xqTitle = ref("查看标准物质详情");
const remainGgForm = ref({});
const data = reactive({
  form: { bzwzJcxmList: [], fileList: [] },
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    qualityLevel: -1,
    drugType: -1
  },
  rules: {
    // packageName: [{ required: true, message: "包名不能为空", trigger: "blur" }],
    // jclbId: [{ required: true, message: "检测类别不能为空", trigger: "blur" }]
  },
});

const { queryParams, form, rules } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

const unitList = ref([]);
getUnitList({ unitGroup: 0 }).then((res) => {
  unitList.value = res.data;
});

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function getList() {
  newListLedger(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      bzwzTable.value = res.rows;
      total.value = res.total;
    }
  );
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// function jcxmCountClick(row) {
//   bzwzList({ bzwzId: row }).then((res) => {
//     jcxmTable.value = res.data;
//     jcxmOpen.value = true;
//   });
// }

function closedDialog() {
  jcxmTable.value = [];
}

function closedXqDialog() {
  form.value = {
    bzwzJcxmList: [],
    fileList: [],
  };
}

function closedRemainGgDialog() {
  remainGgForm.value = {};
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  if (row) {
    const a = proxy.getSupAndSub(row);
    return a;
  }
}

function remainCountClick(row) {
  getRemainGg(row.drugId).then((res) => {
    remainGgForm.value = res.data;
    remainGgOpen.value = true;
  });
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});

function remainGgSave() {
  saveRemainGg(remainGgForm.value).then((res) => {
    remainGgOpen.value = false;
    proxy.$modal.msgSuccess("保存成功");
    getList();
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "/resource/drug/export",
    {
      ...queryParams.value,
    },
    `化学药品入库登记表_${new Date().getTime()}.xlsx`
  );
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
